// pages/compete/launchCom/launchCom.ts
import msg from "../../../utils/msg";
import { create_activity, uploadPic } from "../../../utils/request/api";
import User from "../../../utils/user";
import { formatTime } from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param: {
      //id:"",
      activity_title : "",//活动标题,
      activity_address :"", //活动地点,
      local_and_phone :"", //联系电话,
      apply_number :"", //可报名人数,
      activity_money :"", //活动费用（元）,
      isSignOnline : "641",//收费方式 641-线下报名，642-在线报名
      show_signlist:"1",//是否显示报名名单，1-显示，0-不显示
      //选填项:
      activity_small_pic : "",//缩略图,
      activity_big_pic :"", //滚动图,
      activity_online :"1", //显示前端,
      dispay_star_time :"", //前端显示时间,
      dispay_end_time :"", //前端显示时间,
      sign_up_date :"", //报名时间,
      sign_up_date_end :"", //报名截止时间,
      effective_time :"", //缴费截止日期,
      activity_time :"", //活动开始日期,
      activity_time_end :"", //活动截止日期,
      local_men :"", //联系人,
      intro_content_temp :"", //介绍
      AccessKeyId:'',
      AccessKeySecret:'',
    },
    //时间选择器用到的时间戳
    currentDate: new Date().getTime() as number,
    pickTime: false as boolean,
    pickTimeName: '' as string,
    fileSmallPic: [] as any,
    fileBigPic: [] as any,
    showSuccess:false,
  },
  back(){
    wx.switchTab({
      url:'/pages/act/act',
    })
  },
  submit:async function(){
    if(!this.checkValid()) return;
    msg.Loading('提交中');
    let res:any = await create_activity(this.data.param).then((res)=>{return res;});
    msg.hideLoading();
    if(res.data.error==0){
      this.setData({
        showSuccess:true,
      });
    }
    else{
      msg.show('发布失败',res.data.msg);
    }
  },
  afterReadSmall: async function (event: any) {
    const { file } = event.detail;
    msg.Loading();
    let res = await uploadPic(file.url, 'Filedata')
      .then((res: any) => { return res; });
    if (res.statusCode != 200) {
      msg.show('服务器维护中','请稍后再试');return;
    }
    let data = res.data;
    if (typeof data == 'string') data = JSON.parse(data);
    let fileName = data.savename;
    let fileList = [];
    fileList.push(file);
    this.setData({
      fileSmallPic: fileList,
      ['param.activity_small_pic']: fileName,
    });
    msg.hideLoading();

  },
  //上传前检查数据
  checkValid() {
    let user = new User;
    let param:any = this.data.param;
    param['AccessKeyId'] = user.AccessKey.AccessKeyId;
    param['AccessKeySecret'] = user.AccessKey.AccessKeySecret;
    this.setData({
      param:param,
    });
    for(let key in param){
      let temp = param[key];
      if(!temp) {
        msg.show('请输入完整表单');return false;
      }
    }
    if(this.data.fileSmallPic.length==0){ msg.show('请上传缩略图');return false;}
    if(this.data.fileBigPic.length==0){ msg.show('请上传滚动图');return false;}
    return true;
  },
  //删除图片
  actSmaDel: function () {
    this.setData({
      fileSmallPic: [],
      ['param.activity_small_pic']: '',
    });
  },
  afterReadBig:async function (event: any) {
    const { file } = event.detail;
    msg.Loading();
    let res = await uploadPic(file.url, 'Filedata')
      .then((res: any) => { return res; });
    if (res.statusCode != 200) {
      msg.show('服务器维护中','请稍后再试');return;
    }
    let data = res.data;
    if (typeof data == 'string') data = JSON.parse(data);
    let fileName = data.savename;

    let fileBigPic=this.data.fileBigPic;
    let pic = this.data.param.activity_big_pic;
    if(pic=='') pic=fileName;
    else pic+=','+fileName;
    fileBigPic.push(file);
    this.setData({
      fileBigPic: fileBigPic,
      ['param.activity_big_pic']: pic,
    });
    msg.hideLoading();
  },
  //删除图片
  actBigDel: function (e:any) {
    let index = e.detail.index;
    let fileBigPic = this.data.fileBigPic;
    fileBigPic.splice(index, 1);
    let pics = this.data.param.activity_big_pic.split(',');
    pics.splice(index, 1);
    this.setData({
      fileBigPic: fileBigPic,
      ['param.activity_big_pic']: String(pics),
    });
  },
  onChange2(e: any) {
    this.setData({
      ['param.isSignOnline']: e.detail,
    });
  },
  switchChange(e: any) {
    console.log('e=', e);
    this.setData({
      ['param.show_signlist']: e.detail ? 1 : 0,
    })

  },
  //
  pickTime(e: any) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      pickTime: true,
      pickTimeName: name,
    });
  },
  confirmTime(e: any) {
    let format_time = formatTime(new Date(e.detail));
    this.setData({
      ['param.' + this.data.pickTimeName]: format_time,
    });
    this.onClose();
  },
  onClose() {
    this.setData({
      pickTime: false,
    })
  },
  bindInput(e: any) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      ['param.' + name]: e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})